<?php

require_once (fvSite::$fvConfig->get("path.entity") . 'products.class.php') ;

class ProductsManager extends fvRootManager {
	
	protected function __construct () {
	    $objectClassName = substr(__CLASS__, 0, -7);
	    
	    $this->rootObj = new $objectClassName();
	}
	
    static function getInstance()
    {
        static $instance; 
        if (!isset($instance)) {
            $instance = new self();
        }  
        return $instance;
    }

    public function getImportedData($where, $parameters, $user_id, $operation, $ids = null) {
        $ExportParameters = ExportParametersManager::getInstance()->getAll("user_id = ? and operation = ?", null, null, 
                                   array($user_id, $operation));
                                   
        foreach ($ExportParameters as $ExportParameter) {
            $idsList = $ExportParameter['ids'];
            if (!is_array($idsList)) $idsList = array();

            if (is_array($ids[$ExportParameter['entity_name']])) $idsList = array_intersect($idsList, $ids[$ExportParameter['entity_name']]);
            
            if (count($idsList)) {
                switch ($ExportParameter['entity_name']) {
                	case "Rubric":
                	   $where .= (($where)?" AND ":'') . "rubric_id IN (" . implode(",", $idsList) . ")";
                	break;
                    case "Country":
                       $subWhere = "";
                       foreach ($idsList as $id) {
                           $subWhere .= (($subWhere)?" OR ":'') . "FIND_IN_SET('". fvSite::$DB->escapeSimple($id) . "', countries)";
                       }
                       if ($subWhere)
                        $where .= (($where)?" AND ":'') . $subWhere;
                    break;
                }
            }
        }
        
        return $this->getAll($where, null, null, $parameters);
    }
    
}
